package com.example.sneakerappserver.controller;

import com.example.sneakerappserver.pojo.ResponseMessage;
import com.example.sneakerappserver.pojo.User;
import com.example.sneakerappserver.pojo.dto.UserDto;
import com.example.sneakerappserver.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController // 接口方法返回对象 转换成json文本
@RequestMapping("/user")    // localhost:8088/user/**
public class UserController {

    @Autowired
    IUserService userService;

    // 增加
//    @PostMapping // localhost:8088/user  method post
//    public ResponseMessage<User> add(@RequestBody UserDto user){
//        User userNew = userService.add(user);
//        System.out.print(userNew);
//        return ResponseMessage.success(userNew);
//    }

    @GetMapping("/check") // /check?useName=userName
    public ResponseMessage<Map<String,Boolean>> register (@RequestParam String userName){
        // 检查是否有重名的情况
        User user = userService.queryByUserName(userName);
        boolean exist = user!=null;
        Map<String, Boolean> response = new HashMap<>();
        response.put("exists",exist);
        return ResponseMessage.success(response);
    }



    @PostMapping("/register")
    public ResponseMessage<String> register(@RequestParam String userName, @RequestParam String passWord){
        boolean success = userService.register(userName, passWord);
        if (success) {
            return ResponseMessage.success("注册成功");
        } else {
            return ResponseMessage.success("注册失败");

        }
    }

    // 删除
//    @DeleteMapping
//
//    // 修改 其实就是 save方法，有ID就是修改
//    @PutMapping("/{userId}")
//    public void edit(@PathVariable Integer id){
//
//    }
//
//    // 查询
//    @GetMapping("/{userId}")
//    public ResponseMessage query(@PathVariable Integer id){
//        User user = userService.query(id);
//        System.out.print(user);
//        return ResponseMessage.success(user);
//    }

}
